package com.bluetool.debby.bluetoothjob.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bluetool.debby.bluetoothjob.R;
import com.bluetool.debby.bluetoothjob.listener.OnBlueToothListener;
import com.clj.fastble.data.BleDevice;

import java.util.List;

/**
 * Created by debby on 2018/5/3.
 * 描述:
 */

public class BlueAdapter extends RecyclerView.Adapter<BlueAdapter.ViewHolder> {
    private Context mContext;
    private List<BleDevice> devices;
    private OnBlueToothListener listener;

    public BlueAdapter(Context mContext, List<BleDevice> devices, OnBlueToothListener listener) {
        this.mContext = mContext;
        this.devices = devices;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.adapter_list, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        String name = devices.get(position).getName();
        if (TextUtils.isEmpty(name)) {
            name = devices.get(position).getDevice().toString();
            if (TextUtils.isEmpty(name)) name = "未命名设备";
        }
        holder.tv_name.setText(name);
        holder.tv_address.setText(devices.get(position).getMac());
        holder.tv_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onItemClick(devices.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return devices.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        View mItemView;
        TextView tv_name;
        TextView tv_address;

        ViewHolder(View itemView) {
            super(itemView);
            this.mItemView = itemView;
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_address = itemView.findViewById(R.id.tv_address);
        }
    }

}
